import request from '@utils/request'
import { Gateway, GatewayDTO, GatewaySaveForm } from '@/api/entity/gateway'
import { APIParams, APIResult } from '@utils/request/type'
import { ServerEnum } from '@/api/enum/server'
import settings from '@/settings'
import { getToken } from '@utils/authorization/token'
import { WebSocketPlus } from '@utils/websocket/WebSocketPlus'

/**
 * 分页查询
 * @param params
 */
export const getGatewayListByPageAPI = (params: APIParams.Page<{
  // 模糊查询
  q: string;
  // 网关状态 true-在线 false-离线
  status: boolean;
}>) =>
  request.post<APIResult.Page<GatewayDTO[]>>(ServerEnum.System + '/gateWay/page', params)

/**
 * 获取网关列表
 */
export const getGatewayListAPI = () => request.post<APIResult<Gateway[]>>(ServerEnum.System + '/gateWay/list')

/**
 * 获取网关所在的页数
 * @param params
 */
export const getGatewayPageNumByIdAPI = (params: APIParams.Page<{ id: string }>) => request.post(ServerEnum.System + '/gateWay/getPageNum/', params)

/**
 * 获取网关调试的WebSocket
 * @param params
 */
// _${Authorization.user.id}
export const getDebuggerWebSocketAPI = (params: { gatewayId: string }) => {
  return new WebSocketPlus(settings.URL.WEBSOCKET_URL + ServerEnum.DataGatherer + `/websocket/debugger/${getToken()}/${params.gatewayId}`)
}

/**
 * 添加网关
 * @param params
 */
export const addGatewayAPI = (params: GatewaySaveForm) => request.post(ServerEnum.System + '/gateWay/add', params)

/**
 * 编辑网关
 * @param params
 */
export const editGatewayAPI = (params: GatewaySaveForm) => request.post(ServerEnum.System + '/gateWay/edit', params)

/**
 * 删除网关
 * @param params
 */
export const delGatewayAPI = (params: { id: string }) => request.get(ServerEnum.System + '/gateWay/deleteById/' + params.id)

/**
 * 根据网关编号获取网关
 * @param params
 */
export const getGatewayTypeByNumberAPI = (params: { number: string }) => request.get<APIResult<string>>(ServerEnum.System + '/gateWay/getModelByNumber/' + params.number)

/**
 * 网关转让
 * @param params
 */
export const gatewayTransferAPI = (params: {
  // 网关id
  id: string,
  // 网关名称
  name: string,
  // 分享人手机号
  phone: string
}) => request.post(ServerEnum.System + '/gateWay/transfer', params)
